package boot.mq.rabbitmq.pruducer.config;

import boot.mq.rabbitmq.core.RabbitmqProcessEnum;
import boot.mq.rabbitmq.core.RabbitmqRoutingEnum;
import boot.mq.rabbitmq.core.RabbitmqTopicConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/

@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String man = "topic_man";
    public final static String woman = "topic_woman";

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

//    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    TopicExchange exchangeVacationProcess() {
        return new TopicExchange(RabbitmqProcessEnum.Vacation_Process.getExchange());
    }

//    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.man);
    }

//    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.woman);
    }

    @Bean
    public Queue topicVacationProcessApply() {
        return new Queue(RabbitmqTopicConstant.Vacation_Process_Apply);
    }

    @Bean
    public Queue topicVacationProcessConfirm() {
        return new Queue(RabbitmqTopicConstant.Vacation_Process_Confirm);
    }

    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
//    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
    }

    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
//    Binding bindingExchangeMessage2() {
//        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
//    }
//    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with(woman);
    }

    @Bean
    Binding bindingTopicVacationProcessApply() {
        return BindingBuilder.bind(topicVacationProcessApply()).to(exchangeVacationProcess()).with(RabbitmqRoutingEnum.Vacation_Process_Apply.getRoutingKey());
    }

    @Bean
    Binding bindingTopicVacationProcessConfirm() {
        return BindingBuilder.bind(topicVacationProcessConfirm()).to(exchangeVacationProcess()).with(RabbitmqRoutingEnum.Vacation_Process_Confirm.getRoutingKey());
    }

}
